/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package admin.Controller;

import DTO.Promotion;
import Exception.KwickKioskDBException;
import admin.BusinessLogic.BusinessLogic;
import admin_gui.Admin_EditPromo;
import admin_gui.Admin_ItemToPromo;
import admin_gui.Admin_PromoDetail;
import admin_gui.Admin_SearchPromo;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author WEN
 */
public class PromoController {

    private BusinessLogic businesslogic;
    private JPanel mainPanel;
    private Vector promo;
    private Vector item;
    private Vector wholevector;
    private boolean[] promoItem;
    private boolean[] itemItem;
    private int deleteType = 0;             //to remember the selection type before deleting
    private String searchSelection = "";
    private Vector itemInProm;
    private String promoName;

    public PromoController(JPanel MainPanel) {
        try {
            businesslogic = new BusinessLogic();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(PromoController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(PromoController.class.getName()).log(Level.SEVERE, null, ex);
        }
        mainPanel = MainPanel;
        promo = new Vector();
        promoItem = new boolean[4];
        promoItem[0] = true;
        for (int i = 1; i < 4; i++) {
            promoItem[i] = false;
        }
        item = new Vector();
        itemItem = new boolean[5];
        itemItem[0] = true;
        for (int i = 1; i < 5; i++) {
            itemItem[i] = false;
        }
    }

    public boolean addPromo(String PName, String sDate, String eDate,
            double PAmountD, int storeCard) {
        boolean b = false;
        //    System.out.println("in promocontroller");
        b = businesslogic.addPromo(PName, sDate, eDate, PAmountD, storeCard);
        //   System.out.println("in promocontroller and b is " + b);
        return b;
    }

    public void managePromo(JTable ResultTable, Vector v_head,
            String searchField, boolean[] promoItem) {
   //     System.out.println("searchField:" + searchField);
        this.promoItem = promoItem;
        if (promoItem[0] == true || searchField.length() == 0) {
   //         System.out.println("inmanage");
            showPromoTable(ResultTable, v_head);
        } else if (promoItem[1] == true) {
            showPromoTableByID(searchField.trim(), ResultTable, v_head);
            deleteType = 1;
            searchSelection = searchField.trim();
        } else if (promoItem[2] == true) {
            showPromoTableByStartdate(searchField.trim(), ResultTable, v_head);
            deleteType = 2;
            searchSelection = searchField.trim();
        } else if (promoItem[3] == true) {
            showPromoTableByEnddate(searchField.trim(), ResultTable, v_head);
            deleteType = 3;
            searchSelection = searchField.trim();
        } else {
            JOptionPane.showMessageDialog(null,
                    "Sorry! Can not find that information",
                    "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void showPromoTable(JTable ResultTable, Vector v_head) {
        promo = businesslogic.getPromo();
        DefaultTableModel tm = new DefaultTableModel(promo, v_head);
        ResultTable.setModel(tm);
        ((DefaultTableModel) ResultTable.getModel()).fireTableStructureChanged();
    }

    private void showPromoTableByID(String ID, JTable ResultTable, Vector v_head) {
        promo.clear();
        promo = businesslogic.getPromosByID(ID);
        DefaultTableModel tm = new DefaultTableModel(promo, v_head);
        ResultTable.setModel(tm);
        ((DefaultTableModel) ResultTable.getModel()).fireTableStructureChanged();
    }

    public void showEditPromoPanel(String promoID) {
        wholevector = businesslogic.getPromosByID(promoID);
        Admin_EditPromo editPromoPane = new Admin_EditPromo(this, wholevector);
        editPromoPane.setBounds(1, 1, 762, 613);
        mainPanel.removeAll();
        mainPanel.add(editPromoPane);
        mainPanel.validate();
        mainPanel.repaint();
    }

    public void showPromoDetailPanel(String promoID) {
        wholevector = businesslogic.getPromosByID(promoID);
        promoName = ((Vector) wholevector.elementAt(0)).get(1).toString();
        itemInProm = businesslogic.getItemByPromo(promoID);
        Admin_PromoDetail detailPromoPane = new Admin_PromoDetail(this, promoID,
                promoName, itemInProm);
        detailPromoPane.setBounds(1, 1, 762, 613);
        mainPanel.removeAll();
        mainPanel.add(detailPromoPane);
        mainPanel.validate();
        mainPanel.repaint();
    }

    public void deleteItemFromPromo(String itemID, String promoID) {
        int pid = Integer.parseInt(promoID);
        if (businesslogic.removeItemFromPromotion(itemID, pid)) {
            itemInProm.clear();
            itemInProm = businesslogic.getItemByPromo(promoID);
            Admin_PromoDetail detailPromoPane = new Admin_PromoDetail(this,
                    promoID, promoName, itemInProm);
            detailPromoPane.setBounds(1, 1, 762, 613);
            mainPanel.removeAll();
            mainPanel.add(detailPromoPane);
            mainPanel.validate();
            mainPanel.repaint();
        }
    }

    public void showAddItemPromoPane(String promoID) {
        Admin_ItemToPromo itemToPromoPane = new Admin_ItemToPromo(this, promoID);
        itemToPromoPane.setBounds(1, 1, 762, 613);
        mainPanel.removeAll();
        mainPanel.add(itemToPromoPane);
        mainPanel.validate();
        mainPanel.repaint();
    }

    public void deletePromo(String promoID, JTable ResultTable, Vector v_head) {

        if (businesslogic.deletePromo(promoID)) {
            if (deleteType == 0) {
                showPromoTable(ResultTable, v_head);
            } else if (deleteType == 1) {
                showPromoTableByID(searchSelection, ResultTable, v_head);
            } else if (deleteType == 2) {
                showPromoTableByStartdate(searchSelection, ResultTable, v_head);
            } else {
                showPromoTableByEnddate(searchSelection, ResultTable, v_head);
            }
        }

    }

    private void showPromoTableByStartdate(String Startdate, JTable ResultTable,
            Vector v_head) {
        promo.clear();
        promo = businesslogic.getPromosByStartdate(Startdate);
        DefaultTableModel tm = new DefaultTableModel(promo, v_head);
        ResultTable.setModel(tm);
        ((DefaultTableModel) ResultTable.getModel()).fireTableStructureChanged();
    }

    private void showPromoTableByEnddate(String Enddate, JTable ResultTable,
            Vector v_head) {
        promo.clear();
        promo = businesslogic.getPromosByEnddate(Enddate);
        DefaultTableModel tm = new DefaultTableModel(promo, v_head);
        ResultTable.setModel(tm);
        ((DefaultTableModel) ResultTable.getModel()).fireTableStructureChanged();
    }

    public void updatePromo(Promotion promo) {
        if (businesslogic.updatePromo(promo)) {
            Admin_SearchPromo searchStorePane = new Admin_SearchPromo(mainPanel);
            JOptionPane.showMessageDialog(null,
                    "Promotion has been updated successfully",
                    "Message", JOptionPane.INFORMATION_MESSAGE);
            searchStorePane.setBounds(1, 1, 762, 613);
            mainPanel.removeAll();
            mainPanel.add(searchStorePane);
            mainPanel.validate();
            mainPanel.repaint();
        }
    }

    public void manageItem(JTable ResultTable, Vector v_head, String searchField,
            boolean[] itemItem) {
        this.itemItem = itemItem;
        if (itemItem[0] == true || searchField.length() == 0) {
            showItemTable(ResultTable, v_head);
        } else if (itemItem[1] == true) {
            showItemTableByName(searchField.trim(), ResultTable, v_head);
        } else if (itemItem[2] == true) {
            showItemTableByUPC(searchField.trim(), ResultTable, v_head);
        } else if (itemItem[3] == true) {
            showItemTableByMake(searchField.trim(), ResultTable, v_head);
        } else if (itemItem[4] == true) {
            showItemTableByModel(searchField.trim(), ResultTable, v_head);
        } else {
            JOptionPane.showMessageDialog(null,
                    "Sorry! Can not find that information",
                    "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void showItemTable(JTable ResultTable, Vector v_head) {
        item = businesslogic.getItem();
        DefaultTableModel tm = new DefaultTableModel(item, v_head);
        ResultTable.setModel(tm);
        ((DefaultTableModel) ResultTable.getModel()).fireTableStructureChanged();
    }

    private void showItemTableByName(String name, JTable ResultTable,
            Vector v_head) {
        item.clear();
        item = businesslogic.getItemsByName(name);
        DefaultTableModel tm = new DefaultTableModel(item, v_head);
        ResultTable.setModel(tm);
        ((DefaultTableModel) ResultTable.getModel()).fireTableStructureChanged();
    }

    private void showItemTableByUPC(String UPC, JTable ResultTable,
            Vector v_head) {
        item.clear();
        item = businesslogic.getItemByUPC(UPC);
        DefaultTableModel tm = new DefaultTableModel(item, v_head);
        ResultTable.setModel(tm);
        ((DefaultTableModel) ResultTable.getModel()).fireTableStructureChanged();
    }

    private void showItemTableByMake(String make, JTable ResultTable,
            Vector v_head) {
        item.clear();
        item = businesslogic.getItemsByMake(make);
        DefaultTableModel tm = new DefaultTableModel(item, v_head);
        ResultTable.setModel(tm);
        ((DefaultTableModel) ResultTable.getModel()).fireTableStructureChanged();
    }

    private void showItemTableByModel(String model, JTable ResultTable,
            Vector v_head) {
        item.clear();
        item = businesslogic.getItemsByModel(model);
        DefaultTableModel tm = new DefaultTableModel(item, v_head);
        ResultTable.setModel(tm);
        ((DefaultTableModel) ResultTable.getModel()).fireTableStructureChanged();
    }

    public void addItemToPromotion(String promo, String upc) {
        int pro = Integer.parseInt(promo);
        int checkValue = -1;
        checkValue = businesslogic.addItemToPromotion(upc, pro);
        if (checkValue == 0) {
            JOptionPane.showMessageDialog(null,
                    "The item has been aleady in promotion .",
                    "Error", JOptionPane.ERROR_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(null,
                    "The item has be added into promotion successfully.",
                    "Message", JOptionPane.INFORMATION_MESSAGE);
        }
    }

    public void goBcakSerachPanel() {
        Admin_SearchPromo searchStorePane = new Admin_SearchPromo(mainPanel);
        searchStorePane.setBounds(1, 1, 762, 613);
        mainPanel.removeAll();
        mainPanel.add(searchStorePane);
        mainPanel.validate();
        mainPanel.repaint();
    }
}
